package com.example.dangerouscargodriver.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.entry.CompanyTruckInfo;
import com.example.dangerouscargodriver.ui.activity.CompanyTruckDetailInfoActivity;

import java.util.ArrayList;
import java.util.List;

public class TruckInfoAdapter extends RecyclerView.Adapter<TruckInfoAdapter.ViewHolder>{

	private static final String TAG = "CompanyInfoAdapter";
	private List<CompanyTruckInfo> mCarInfoItemList = null;
	private Context mContext;
	private ClickListener mClickListener;
	private View mRootView;

	static class ViewHolder extends RecyclerView.ViewHolder {
		private TextView mCarNo = null;
		private TextView mCarType = null;
		private TextView mDeliverNum = null;
		private LinearLayout mContent = null;
		private ImageView mMainPic = null;
		private TextView mTruckStatus = null;

		public ViewHolder(View view) {
			super(view);
			mCarNo = view.findViewById(R.id.tv_car_no);
			mDeliverNum = view.findViewById(R.id.tv_text_deliver_num);
			mCarType = view.findViewById(R.id.tv_car_type);
			mContent = view.findViewById(R.id.layout_truck_content);
			mMainPic = view.findViewById(R.id.iv_main_pic);
			mTruckStatus = view.findViewById(R.id.tv_text_car_status);
		}

	}

	public TruckInfoAdapter() {
		mCarInfoItemList = new ArrayList<>();
		mClickListener = new ClickListener();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mRootView = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.layout_car_info_item, parent, false);
		ViewHolder holder = new ViewHolder(mRootView);

		return holder;
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		final int selectPos = position;
		CompanyTruckInfo carDetailInfo = mCarInfoItemList.get(position);

		holder.mCarNo.setText(carDetailInfo.getTruckno());
		holder.mCarType.setText(carDetailInfo.getClassname());
		//设置star图片信息
		holder.mDeliverNum.setText(carDetailInfo.getTotal_trans());
		Glide.with(mRootView.getContext()).load(carDetailInfo.getMain_pic()).into(holder.mMainPic);
		holder.mTruckStatus.setText(carDetailInfo.getAdultstatus_name());
		holder.mContent.removeAllViews();
		if(carDetailInfo.getWeight() != null && !carDetailInfo.getWeight().equals("")){
			View layout = LayoutInflater.from(mRootView.getContext()).inflate(
					R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(carDetailInfo.getWeight() + "吨");
			holder.mContent.addView(layout);
		}

		if(carDetailInfo.getCore_attr() != null && !carDetailInfo.getCore_attr().equals("")){
			String core_attr = carDetailInfo.getCore_attr();
			String[] array = core_attr.split(",");
			for(int index = 0; index < array.length; index++) {
				View layout = LayoutInflater.from(mRootView.getContext()).inflate(
						R.layout.layout_resource_meta_info_item, null);
				TextView textView = layout.findViewById(R.id.tv_item_meta_string);
				textView.setText(array[index]);
				holder.mContent.addView(layout);
			}
		}

		holder.itemView.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				mClickListener.onClick(selectPos);
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public int getItemCount() {
		return mCarInfoItemList.size();
	}

	public class ClickListener {
		private int mPosition = -1;

		void onClick(int position){
			mPosition = position;
			Intent intent = new Intent(mRootView.getContext(), CompanyTruckDetailInfoActivity.class);
			intent.putExtra("tid", mCarInfoItemList.get(position).getTid());
			mRootView.getContext().startActivity(intent);
		}

		public int getPosition() {

			return mPosition;
		}

		public void setPosition(int mPosition) {

			this.mPosition = mPosition;
		}
	}

	public void addData(int position, CompanyTruckInfo item) {
		mCarInfoItemList.add(position, item);
		notifyItemInserted(position);
	}

	public void removeData(int position) {
		mCarInfoItemList.remove(position);
		notifyItemRemoved(position);
	}

	public void updateData(int position, CompanyTruckInfo item) {
		mCarInfoItemList.set(position, item);
		notifyItemChanged(position);
	}

	public void clearData(){
		mCarInfoItemList.clear();
		notifyDataSetChanged();
	}

	public void appendData(List<CompanyTruckInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mCarInfoItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}
}
